package com.yuandian.utils.pool;

import java.util.concurrent.*;

/**
 * 线程池工具类
 *
 * @author: tangqingchun
 * @date: 2020-09-11
 */
public class ExecutorUtils {

    /**
     * 默认核心线程数
     */
    private static final int corePoolSize = 1;

    /**
     * 默认最大线程数
     */
    private static final int maxPoolSize = Runtime.getRuntime().availableProcessors() + corePoolSize;

    /**
     * 线程池维护线程所允许的空闲时间(ms)
     */
    private static final long keepAlive = 3000;

    /**
     * 默认队列容量
     */
    private static final int queueCapacity = 100;

    /**
     * 公共线程池
     */
    private static final ExecutorService EXECUTOR_SERVICE;

    static {
        EXECUTOR_SERVICE = new ThreadPoolExecutor(corePoolSize, maxPoolSize,
                keepAlive, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(queueCapacity));
    }

    /**
     * 设置不能实例化
     */
    private ExecutorUtils(){}

    /**
     * 获取默认的公共线程池
     * @return
     */
    public static ExecutorService getCommonThreadPool(){
        return EXECUTOR_SERVICE;
    }

    /**
     * 创建默认参数线程池
     *
     * @return
     */
    public static ExecutorService defaultThreadPool() {
        return new ThreadPoolExecutor(corePoolSize, maxPoolSize,
                keepAlive, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(queueCapacity));
    }

    /**
     * 创建线程池
     *
     * @param coreSize 核心线程数
     * @param maxSize 最大线程数
     * @return
     */
    public static ExecutorService defaultThreadPool(int coreSize, int maxSize) {
        return new ThreadPoolExecutor(coreSize,
                maxSize,
                keepAlive,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(queueCapacity));
    }

    /**
     * 创建线程池
     *
     * @param coreSize 核心线程数
     * @param maxSize 最大线程数
     * @param keepAliveTime 空闲线程存活时间
     * @param unit 空闲线程存活时间单位
     * @return
     */
    public static ExecutorService defaultThreadPool(int coreSize, int maxSize, long keepAliveTime, TimeUnit unit) {
        return new ThreadPoolExecutor(coreSize,
                maxSize,
                keepAliveTime,
                unit,
                new LinkedBlockingQueue<Runnable>(queueCapacity));
    }

    /**
     * 创建线程池
     * @param coreSize 核心线程数
     * @param maxSize 最大线程数
     * @param keepAliveTime 空闲线程存活时间
     * @param unit 空闲线程存活时间单位
     * @param workQueue 工作队列
     * @param threadFactory 线程创建工厂
     * @return
     */
    public static ExecutorService defaultThreadPool(int coreSize,
                                                    int maxSize,
                                                    long keepAliveTime,
                                                    TimeUnit unit,
                                                    BlockingQueue<Runnable> workQueue,
                                                    ThreadFactory threadFactory) {
        return new ThreadPoolExecutor(coreSize, maxSize, keepAliveTime, unit, workQueue, threadFactory);
    }

    /**
     * 创建线程池
     *
     * @param coreSize 核心线程数
     * @param maxSize 最大线程数
     * @param keepAliveTime 空闲线程存活时间
     * @param unit 空闲线程存活时间单位
     * @param workQueue 工作队列
     * @param threadFactory 线程创建工厂
     * @param handler 拒绝策略
     * @return
     */
    public static ExecutorService defaultThreadPool(int coreSize,
                                                    int maxSize,
                                                    long keepAliveTime,
                                                    TimeUnit unit,
                                                    BlockingQueue<Runnable> workQueue,
                                                    ThreadFactory threadFactory,
                                                    RejectedExecutionHandler handler) {
        return new ThreadPoolExecutor(coreSize, maxSize, keepAliveTime, unit, workQueue, threadFactory, handler);
    }

}
